﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common.XmlConfig
{
    public class ConfigGroup : BaseConfig
    {
        private ConfigList<ConfigSetting> _Settings;
        private ConfigList<ConfigGroup> _Groups;

        /// <summary>
        /// 创建新组
        /// </summary>
        /// <param name="key"></param>
        /// <param name="text"></param>
        public ConfigGroup(string key, string text)
        {
            this._Key = key;
            this.Text = text;
            this._Node = null;
        }
        /// <summary>
        /// Xml节点
        /// </summary>
        /// <param name="Node"></param>
        public ConfigGroup(XmlNode Node)
        {
            if (Node.Attributes["Key"] == null) throw new Exception("属性Key值为空，");
            if (Node.Attributes["Text"] == null) throw new Exception("属性Text值为空，");
            this._Key = Node.Attributes["Key"].Value;
            this.Text = Node.Attributes["Text"] == null ? null : Node.Attributes["Text"].Value;
            this._Node = Node;
        }

        private ConfigGroup() { }

        /// <summary>
        /// 配置信息
        /// </summary>
        public ConfigList<ConfigSetting> Settings
        {
            get
            {
                if (_Settings == null)
                {
                    _Settings = new ConfigList<ConfigSetting>();
                }
                return _Settings;
            }
        }

        /// <summary>
        /// 子分组
        /// </summary>
        public ConfigList<ConfigGroup> Groups
        {
            get
            {
                if (_Groups == null)
                {
                    _Groups = new ConfigList<ConfigGroup>();
                }
                return _Groups;
            }
        }

        /// <summary>
        /// 添加配置信息
        /// </summary>
        /// <param name="setting"></param>
        internal void AddSetting(ConfigSetting setting)
        {
            setting.SetParentGroup(this);
            Settings.Add(setting);
        }

        /// <summary>
        /// 添加子分组
        /// </summary>
        /// <param name="group"></param>
        internal void AddGroup(ConfigGroup group)
        {
            group.SetParentGroup(this);
            Groups.Add(group);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Key:" + this.Key + ",Text:" + this.Text;
        }
    }
}
